// 只做数据库处理
const mysql2 = require("mysql2");

const UserServer = {
    addUser:(datalist)=>{
         // 创建数据库链接
      const config = getDBConfig();
      const promisePool = mysql2.createPool(config).promise();
      // 模板语法
      const index = `INSERT INTO picture(name,user_id, type) VALUES (?,?,?)`

      const users =  promisePool.query(index,datalist)
    },

    updataUser : (up_id,data)=>{
        const config = getDBConfig();
      const promisePool = mysql2.createPool(config).promise();
    
      const updata = `UPDATE picture SET name=? WHERE id="${up_id}" `;
    
      const users =  promisePool.query(updata,data)
    },

    selectUser : ()=>{
        const config = getDBConfig();
        const promisePool = mysql2.createPool(config).promise();
      

        // const promisePool = ServerLink();
        const select = `SELECT * FROM picture `;

        return promisePool.query(select)
    },

    deleteUser : (del_id)=>{
      const config = getDBConfig();
      const promisePool = mysql2.createPool(config).promise();

      const deletes = `DELETE FROM picture WHERE id=?`;
    
      const users =  promisePool.query(deletes,del_id)
    }
}

module.exports = UserServer

// 连接数据库  -- 想进行错误处理  失败
function ServerLink(){
  try{
    const config = getDBConfig();
    var promisePool = mysql2.createPool(config).promise();
  }catch(err){
    console.log("数据库连接失败");
  }
  return promisePool;
}

// 连接数据库的信息
function getDBConfig()
{
    return {
        host:'127.0.0.1',
        port:3306,    // 这是myspl的端口，默认的
        user:"root",
        password:"",
        database:"guyueyan",
        connectionLimit:1
    }
}